from marshmallow import Schema, fields, validate, validates, ValidationError
from datetime import datetime

class ReportParamsSchema(Schema):
    """报表参数模式"""
    startDate = fields.DateTime()
    endDate = fields.DateTime()
    groupBy = fields.Str()
    sortBy = fields.Str()
    sortOrder = fields.Str(validate=validate.OneOf(['asc', 'desc']))
    limit = fields.Int()

class ScheduleSchema(Schema):
    """报表计划模式"""
    isScheduled = fields.Bool(dump_default=False)
    frequency = fields.Str(validate=validate.OneOf(['daily', 'weekly', 'monthly', 'quarterly', 'yearly']))
    nextRunTime = fields.DateTime()

class LastRunSchema(Schema):
    """上次运行记录模式"""
    time = fields.DateTime()
    status = fields.Str()
    message = fields.Str()
    resultUrl = fields.Str()

class ReportSchema(Schema):
    """报表模式"""
    name = fields.Str(required=True, validate=validate.Length(min=2, max=100))
    type = fields.Str(required=True, validate=validate.OneOf(['患者统计', '处方统计', '药品统计', '收入统计', '医生工作量统计', '自定义']))
    description = fields.Str(validate=validate.Length(max=500))
    parameters = fields.Nested(ReportParamsSchema)
    dataSources = fields.List(fields.Str())
    createdBy = fields.Str()
    schedule = fields.Nested(ScheduleSchema)
    lastRun = fields.Nested(LastRunSchema)
    status = fields.Str(validate=validate.OneOf(['已启用', '已禁用']), dump_default='已启用')
    createdAt = fields.DateTime(dump_default=datetime.utcnow)
    updatedAt = fields.DateTime(dump_default=datetime.utcnow)

class ReportUpdateSchema(Schema):
    """报表更新模式"""
    name = fields.Str(validate=validate.Length(min=2, max=100))
    type = fields.Str(validate=validate.OneOf(['患者统计', '处方统计', '药品统计', '收入统计', '医生工作量统计', '自定义']))
    description = fields.Str(validate=validate.Length(max=500))
    parameters = fields.Nested(ReportParamsSchema)
    dataSources = fields.List(fields.Str())
    schedule = fields.Nested(ScheduleSchema)
    status = fields.Str(validate=validate.OneOf(['已启用', '已禁用'])) 